package android.medicalcloud.com.yiapp.yilian.ui;

import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.activity.BaseActivity;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.StringTools;
import android.medicalcloud.com.yiapp.yilian.utils.android.PhoneTextIdentify;
import android.medicalcloud.com.yiapp.yilian.utils.android.ToastTool;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


/**
 * Created by wanggenlin on 17-5-18.
 */
public class FindPassActivity extends BaseActivity {

    EditText userNameEt,mobileEt,veryEt,newPasEt,confirePasEt;
    TextView smsSend;
    int count = 120;
    private final int DISABLE_STAUES = 1;
    private final int ABLE_STAUES = 0;
    @Override
    protected void initView() {
        handler = new CountHandler();
        userNameEt = (EditText)findViewById(R.id.userNameEt);
        mobileEt = (EditText)findViewById(R.id.mobileEt);
        veryEt = (EditText)findViewById(R.id.veryEt);
        newPasEt = (EditText)findViewById(R.id.newPasEt);

        confirePasEt = (EditText)findViewById(R.id.confirePasEt);
        smsSend = (TextView) findViewById(R.id.smsSend);
        findViewById(R.id.backIv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.loginBt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String mobile = mobileEt.getText().toString();
                final String name = userNameEt.getText().toString();
                String code = veryEt.getText().toString();
                final String psw = newPasEt.getText().toString();
                final String confirePas = confirePasEt.getText().toString();
                if (StringTools.isNullOrEmpty(name)) {
                    ToastTool.show("姓名不能为空");
                    return;
                }
                if (StringTools.isNullOrEmpty(mobile)) {
                    ToastTool.show("手机号码不能为空");
                    return;
                }
                if (!PhoneTextIdentify.isMobileNO(mobile)) {
                    ToastTool.show("手机号码格式不正确");
                    return;
                }
                if (StringTools.isNullOrEmpty(code)) {
                    ToastTool.show("验证码不能为空");
                    return;
                }
                if (StringTools.isNullOrEmpty(code)) {
                    ToastTool.show("验证码不正确");
                    return;
                }
                if (StringTools.isNullOrEmpty(psw)) {
                    ToastTool.show("请输入密码");
                    return;
                }
                if (psw.length() < 6 || psw.length() > 16) {
                    ToastTool.show("请输入6-16位的密码");
                    return;
                }
                if (StringTools.isNullOrEmpty(confirePas)) {
                    ToastTool.show("请输入密码");
                    return;
                }
                if (confirePas.length() < 6 || confirePas.length() > 16) {
                    ToastTool.show("请输入6-16位的密码");
                    return;
                }
                if (!psw.equals(confirePas)) {
                    ToastTool.show("确认密码错误");
                    return;
                }
                finish();
            }
        });
        smsSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (smsSend.isEnabled()) {
                    String mobile = mobileEt.getText().toString();
                    if (StringTools.isNullOrEmpty(mobile)) {
                        ToastTool.show("手机号码不能为空");
                        return;
                    }
                    if (!PhoneTextIdentify.isMobileNO(mobile)) {
                        ToastTool.show("手机号码格式不正确");
                        return;
                    }
                    new CountThread().start();
//                    new CountThread().start();
//                    UserController.getInstance().sendPinCode(new ViewNetCallBack() {
//                        @Override
//                        public void onConnectStart(Object o) {
//                            smsSend.setEnabled(false);
//                        }
//
//                        @Override
//                        public void onConnectEnd() {
//
//                        }
//
//                        @Override
//                        public void onFail(Exception e, Object o, String reslut) {
//                            smsSend.setEnabled(true);
//                        }
//
//                        @Override
//                        public void onData(Serializable result, int flag, boolean fromNet, Object o) {
//                            try {
//                                JSONObject jsonObject = new JSONObject(o.toString());
//                                if (jsonObject.getInt("code") == 0) {
//                                    new CountThread().start();
//                                } else {
//                                    ToastTool.show(jsonObject.getString("message"));
//                                    smsSend.setEnabled(true);
//                                }
//
//                            } catch (Exception e1) {
//                                e1.printStackTrace();
//                            }
//                        }
//                    }, mobile, 2);
                }
            }
        });


    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.ac_findpassword;
    }

    CountHandler handler;

    private class CountHandler extends Handler {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);

            if (msg.what == DISABLE_STAUES) {

                smsSend.setText(  count + "s");
                count = count - 1;
//                smsSend.setTextColor(getResources().getColor(R.color.color_c2c2c2));
//                setBackgroundColor(getResources().getColor(R.color.selected_line_color));
                smsSend.setEnabled(false);

            }
            if (msg.what == ABLE_STAUES) {
                count = 60;
                smsSend.setText("发送验证码");
//                setBackgroundResource(R.drawable.common_btn_bg);
//                smsSend.setTextColor(getResources().getColor(R.color.color_66666));
                smsSend.setEnabled(true);

            }
        }
    }

    private class CountThread extends Thread {
        @Override
        public void run() {
            super.run();

            while (count > 0) {
                handler.sendEmptyMessage(DISABLE_STAUES);
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    Logger.e(e.getLocalizedMessage(), e);
                }

            }

            handler.sendEmptyMessage(ABLE_STAUES);

        }
    }

}
